﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace GameEngine.Clients.Silverlight.View
{
    using PureMVC.Interfaces;
    using System.Collections.Generic;
    using PureMVC.Patterns;
    using GameEngine.Clients.Silverlight.View.Components;

    /// <summary>
    /// Catches KeyEvents
    /// </summary>
    public class DesktopMediator : FrameworkElementMediator
    {
        public static String MEDIATORNAME = "DesktopMediator";
        public const String SET_SCREEN = "DesktopMediator_setScreen";
        public const String KEY_UP = "DesktopMediator_keyUp";
        public const String KEY_DOWN = "DesktopMediator_keyDown";
        FrameworkElement CurrentScreen;

        /// <summary>
        /// Tem todos as partes visiveis da aplicação
        /// </summary>
        /// <param name="viewComponent"></param>
        public DesktopMediator()
            : base(MEDIATORNAME, new DesktopUC()) 
        {
        }

        public new DesktopUC ViewComponent
        {
            get { return base.ViewComponent as DesktopUC; }
        }

        #region IMediator Members

        /// <summary>
        /// List the <c>INotification</c> names this <c>Mediator</c> is interested in being notified of
        /// </summary>
        /// <returns>The list of <c>INotification</c> names </returns>
        override public IList<string> ListNotificationInterests()
        {
            return new List<string>() { SET_SCREEN };
        }

        /// <summary>
        /// Handle <c>INotification</c>s
        /// </summary>
        /// <param name="notification">The <c>INotification</c> instance to handle</param>
        /// <remarks>
        ///     <para>
        ///        Typically this will be handled in a switch statement, with one 'case' entry per <c>INotification</c> the <c>Mediator</c> is interested in. 
        ///     </para>
        /// </remarks>
        override public void HandleNotification(INotification notification)
        {
            switch (notification.Name) { 
                case SET_SCREEN:
                    setScreen(notification.Body as FrameworkElement);
                    break;
            }
        }

        /// <summary>
        /// Called by the View when the Mediator is registered
        /// </summary>
        override public void OnRegister()
        {
            ViewComponent.OnKeyUpEvent += OnKeyUp;
            ViewComponent.OnKeyDownEvent += OnKeyDown;
        }

        /// <summary>
        /// Called by the View when the Mediator is removed
        /// </summary>
        override public void OnRemove()
        {
            //Facade.RemoveMediator(CurrentScreen.MediatorName);
        }

        #endregion

        private void setScreen(FrameworkElement component)
        {
            /*if (CurrentScreen != null)
            {
                Facade.RemoveMediator(CurrentScreen.MediatorName);
            }*/
            CurrentScreen = component;
            ViewComponent.setScreen(component);
            //MessageBox.Show(component.MediatorName);
        }

        private void OnKeyUp(KeyEventArgs key)
        {
            SendNotification(KEY_UP, key);
        }

        private void OnKeyDown(KeyEventArgs key)
        {
            SendNotification(KEY_DOWN, key);
        }

    }
}